package it.polimi.travelDreamEJB.web.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.polimi.travelDreamEJB.basicproductmanagement.BasicProductMgr;
import it.polimi.travelDreamEJB.basicproductmanagement.dto.ExcursionDTO;
import it.polimi.travelDreamEJB.basicproductmanagement.dto.FlightDTO;
import it.polimi.travelDreamEJB.basicproductmanagement.dto.HotelDTO;
import it.polimi.travelDreamEJB.basicproductmanagement.dto.SearchExcursionDTO;
import it.polimi.travelDreamEJB.basicproductmanagement.dto.SearchFlightDTO;
import it.polimi.travelDreamEJB.basicproductmanagement.dto.SearchHotelDTO;
import it.polimi.travelDreamEJB.defaultpacketmanagement.DefaultPacketMgr;
import it.polimi.travelDreamEJB.defaultpacketmanagement.dto.DefaultPacketDTO;
import it.polimi.travelDreamEJB.personalizedpacketmanagement.PersonalizedPacketMgr;
import it.polimi.travelDreamEJB.personalizedpacketmanagement.dto.PersonalizedPacketDTO;
import it.polimi.travelDreamEJB.personalizedpacketmanagement.dto.SearchPersonalizedPacketDTO;
import it.polimi.travelDreamEJB.usermanagement.UserMgr;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.validation.constraints.Pattern;

@ManagedBean(name = "personalizedPacketBean")
@ViewScoped
public class PersonalizedPacketBean {
	/*
	 * @EJB private DefaultPacketMgr dpm;
	 */

	@EJB
	private PersonalizedPacketMgr ppm;

	@EJB
	private UserMgr user;

	@EJB
	private BasicProductMgr bpm;

	private DefaultPacketDTO selectedDefaultPacket;
	private PersonalizedPacketDTO personalizedPacket;

	private SearchPersonalizedPacketDTO searchPersonalizedPacket;

	private List<PersonalizedPacketDTO> filterPersonalizedPacketList;
	private List<PersonalizedPacketDTO> personalizedPacketList;

	private List<String> userList;
	private List<String> cities;

	private String destinationSelected;

	private String email;

	private HotelDTO hotel;
	private FlightDTO departureFlight;
	private FlightDTO returnFlight;
	private List<ExcursionDTO> excursions;
	private int totalPrice;

	private SearchHotelDTO searchHotel;
	private List<HotelDTO> hotelList;

	private SearchExcursionDTO searchExcursion;
	private List<ExcursionDTO> excursionList;

	private SearchFlightDTO searchDepartureFlight;
	private List<FlightDTO> departureFlightList;

	private SearchFlightDTO searchReturnFlight;
	private List<FlightDTO> returnFlightList;

	private Date until;

	private String hotelName;
	private String creatorName;
	private String departureF;
	private String returnF;

	public PersonalizedPacketBean() {

		filterPersonalizedPacketList = new ArrayList<PersonalizedPacketDTO>();
		userList = new ArrayList<String>();
		until = new Date();
		cities = new ArrayList<String>();
		searchHotel = new SearchHotelDTO();
		searchReturnFlight = new SearchFlightDTO();
		searchDepartureFlight = new SearchFlightDTO();
		searchExcursion = new SearchExcursionDTO();
		hotel = new HotelDTO();
		hotel.setStars(1);
		departureFlight = new FlightDTO();
		returnFlight = new FlightDTO();
		personalizedPacket = new PersonalizedPacketDTO();
		searchPersonalizedPacket = new SearchPersonalizedPacketDTO();
		selectedDefaultPacket = new DefaultPacketDTO();

	}

	
	public List<String> getUserList() {
		return userList;
	}


	public void setUserList(List<String> userList) {
		this.userList = userList;
	}


	// Getter e Setter
	public PersonalizedPacketDTO getPersonalizedPacket() {
		return personalizedPacket;
	}

	public SearchPersonalizedPacketDTO getSearchPersonalizedPacket() {
		return searchPersonalizedPacket;
	}

	public List<PersonalizedPacketDTO> getPersonalizedPacketList() {
		return personalizedPacketList;
	}

	public void setPersonalizedPacket(PersonalizedPacketDTO personalizedPacket) {
		this.personalizedPacket = personalizedPacket;
	}

	public void setSearchPersonalizedPacket(
			SearchPersonalizedPacketDTO searchPersonalizedPacket) {
		this.searchPersonalizedPacket = searchPersonalizedPacket;
	}

	public void setPersonalizedPacketList(
			List<PersonalizedPacketDTO> personalizedPacketList) {
		this.personalizedPacketList = personalizedPacketList;
	}

	public HotelDTO getHotel() {
		return hotel;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		if (email.equals(user.getUserDTO().getEmail())) {
			return;
		}
		this.email = email;
	}

	public void setHotel(HotelDTO hotel) {
		this.hotel = hotel;
	}

	public FlightDTO getDepartureFlight() {
		return departureFlight;
	}

	public FlightDTO getReturnFlight() {
		return returnFlight;
	}

	public void setDepartureFlight(FlightDTO departureFlight) {
		this.departureFlight = departureFlight;
	}

	public void setReturnFlight(FlightDTO returnFlight) {
		this.returnFlight = returnFlight;
	}

	public List<String> getCities() {
		return cities;
	}

	public void setCities(List<String> cities) {
		this.cities = cities;
	}

	public String getDestinationSelected() {
		return destinationSelected;
	}

	public void setDestinationSelected(String destinationSelected) {
		this.destinationSelected = destinationSelected;
	}

	public List<ExcursionDTO> getExcursions() {
		return excursions;
	}

	public void setExcursions(List<ExcursionDTO> excursions) {
		this.excursions = excursions;
	}

	public SearchHotelDTO getSearchHotel() {
		return searchHotel;
	}

	public void setSearchHotel(SearchHotelDTO searchHotel) {
		this.searchHotel = searchHotel;
	}

	public List<HotelDTO> getHotelList() {
		return hotelList;
	}

	public void setHotelList(List<HotelDTO> hotelList) {
		this.hotelList = hotelList;
	}

	public SearchExcursionDTO getSearchExcursion() {
		return searchExcursion;
	}

	public void setSearchExcursion(SearchExcursionDTO searchExcursion) {
		this.searchExcursion = searchExcursion;
	}

	public List<ExcursionDTO> getExcursionList() {
		return excursionList;
	}

	public void setExcursionList(List<ExcursionDTO> excursionList) {
		this.excursionList = excursionList;
	}

	public SearchFlightDTO getSearchDepartureFlight() {
		return searchDepartureFlight;
	}
	
	public List<PersonalizedPacketDTO> getFilterPersonalizedPacketList() {
		return filterPersonalizedPacketList;
	}


	public void setFilterPersonalizedPacketList(
			List<PersonalizedPacketDTO> filterPersonalizedPacketList) {
		this.filterPersonalizedPacketList = filterPersonalizedPacketList;
	}


	public void setSearchDepartureFlight(SearchFlightDTO searchDepartureFlight) {
		this.searchDepartureFlight = searchDepartureFlight;
	}

	public List<FlightDTO> getDepartureFlightList() {
		return departureFlightList;
	}

	public void setDepartureFlightList(List<FlightDTO> departureFlightList) {
		this.departureFlightList = departureFlightList;
	}

	public SearchFlightDTO getSearchReturnFlight() {
		return searchReturnFlight;
	}

	public void setSearchReturnFlight(SearchFlightDTO searchReturnFlight) {
		this.searchReturnFlight = searchReturnFlight;
	}

	public List<FlightDTO> getReturnFlightList() {
		return returnFlightList;
	}

	public void setReturnFlightList(List<FlightDTO> returnFlightList) {
		this.returnFlightList = returnFlightList;
	}

	public Date getUntil() {
		return until;
	}

	public void setUntil(Date until) {
		this.until = until;
	}

	public String getCreatorName() {
		return creatorName;
	}

	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}

	public String getHotelName() {
		return hotelName;
	}

	public void setHotelName(String hotelName) {
		this.hotelName = hotelName;
	}

	public String getDepartureF() {
		return departureF;
	}

	public void setDepartureF(String departureF) {
		this.departureF = departureF;
	}

	public String getReturnF() {
		return returnF;
	}

	public void setReturnF(String returnF) {
		this.returnF = returnF;
	}

	// show possibles cities to create a personalized packet
	public void reloadPossibleCities() {
		cities.clear();
		cities.addAll(bpm.possibleCities());
	}

	// save personalized packet
	public String save() {
		personalizedPacket.setUser(user.getUserDTO());
		personalizedPacket.setHotel(hotel);
		personalizedPacket.setDepartureFlight(departureFlight);
		personalizedPacket.setReturnFlight(returnFlight);
		personalizedPacket.setExcursions(excursions);
		ppm.save_personalized_packet(personalizedPacket);
		return "packet?faces-redirect=true"; // torna alla pagina packet
	}

	// search user's personalized packet
	public void find() {
		SearchPersonalizedPacketDTO searchPersonalizedPacket = new SearchPersonalizedPacketDTO();
		searchPersonalizedPacket.setUser(user.getUserDTO());
		searchPersonalizedPacket.getHotel().setDestination(destinationSelected);
		searchPersonalizedPacket.getHotel().setName(hotelName);
		searchPersonalizedPacket.getDeparture_flight().setDestination(
				destinationSelected);
		searchPersonalizedPacket.getReturn_flight().setDeparture(
				destinationSelected);
		personalizedPacketList = ppm
				.findPersonalizedPacket(searchPersonalizedPacket);
	}
	
	//search personalized packet 
	public void findPersonalizedPacket() {
		SearchPersonalizedPacketDTO searchPersonalizedPacket = new SearchPersonalizedPacketDTO();
		searchPersonalizedPacket.getUser().setEmail(creatorName);
		personalizedPacketList = ppm
				.findPersonalizedPacket(searchPersonalizedPacket);
		filterPersonalizedPacketList.clear();
		for ( PersonalizedPacketDTO pp : personalizedPacketList ){
			filterPersonalizedPacketList.add(pp);
		}
	}
	
	// delete personalized packet
	public String delete() {
		ppm.delete_personalized_packet(personalizedPacket);
		return "packet?faces-redirect=true";
	}

	// search basic products for a specific destination
	public void findBasicProducts() {
		if (destinationSelected.equals("")) {
			return;
		}
		hotel = new HotelDTO();
		excursions.clear();

		searchHotel.setDestination(destinationSelected);
		hotelList = bpm.findHotel(searchHotel);

		searchExcursion.setDestination(destinationSelected);
		excursionList = bpm.findExcursion(searchExcursion);

		searchDepartureFlight.setDestination(destinationSelected);
		searchDepartureFlight.setToFlightDate(until);
		departureFlightList = bpm.findFlight(searchDepartureFlight);

		searchReturnFlight.setDeparture(destinationSelected);
		searchReturnFlight.setToFlightDate(until);
		returnFlightList = bpm.findFlight(searchReturnFlight);
	}

	// calculate total price
	public void calculateNewPacketPrice() {
		totalPrice = hotel.getPrice() + returnFlight.getPrice()
				+ departureFlight.getPrice();
		for (ExcursionDTO e : excursions) {
			totalPrice += e.getPrice();
		}
	}

	public void calculatePacketPrice() {
		totalPrice = personalizedPacket.getHotel().getPrice()
				+ personalizedPacket.getDepartureFlight().getPrice()
				+ personalizedPacket.getReturnFlight().getPrice();
		for (ExcursionDTO e : personalizedPacket.getExcursions()) {
			totalPrice += e.getPrice();
		}
	}

	public String buyIt() {

		if (personalizedPacket.getUser().getEmail()
				.equals(user.getUserDTO().getEmail())) {
			ppm.buyPacket(personalizedPacket, user.getUserDTO());
		} else {

			personalizedPacket.setUser(user.getUserDTO());
			ppm.save_and_buy_personalized_packet(personalizedPacket);

		}

		return "packet?faces-redirect=true";
	}

	public void invite() {

		ppm.inviteAtPacket(personalizedPacket, email);

	}

	public void findGuestPacket() {

		searchPersonalizedPacket.setUser(user.getUserDTO());
		personalizedPacketList = ppm.findGuestPacket(searchPersonalizedPacket);

	}

	public void findBoughtPacket() {

		searchPersonalizedPacket.setUser(user.getUserDTO());
		personalizedPacketList = ppm.findBoughtPacket(searchPersonalizedPacket);

	}

	public void findFlightForPersonalizedPacket() {

		searchDepartureFlight.setDestination(selectedDefaultPacket.getHotel()
				.getDestination());
		searchDepartureFlight.setToFlightDate(until);
		departureFlightList = bpm.findFlight(searchDepartureFlight);

		searchReturnFlight.setDeparture(selectedDefaultPacket.getHotel()
				.getDestination());
		searchReturnFlight.setToFlightDate(until);
		returnFlightList = bpm.findFlight(searchReturnFlight);

	}

	public String saveFromDefaultPacket() {

		personalizedPacket.setUser(user.getUserDTO());
		personalizedPacket.setHotel(selectedDefaultPacket.getHotel());
		personalizedPacket.setDepartureFlight(departureFlight);
		personalizedPacket.setReturnFlight(returnFlight);
		personalizedPacket.setExcursions(selectedDefaultPacket.getExcursions());
		ppm.save_personalized_packet(personalizedPacket);
		return "packet?faces-redirect=true";

	}

	public DefaultPacketDTO getSelectedDefaultPacket() {
		return selectedDefaultPacket;
	}

	public void setSelectedDefaultPacket(DefaultPacketDTO selectedDefaultPacket) {
		this.selectedDefaultPacket = selectedDefaultPacket;
	}

	public int getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(int totalPrice) {
		this.totalPrice = totalPrice;
	}

	public void findUsers(){
		
		userList = user.findRegisteredUser();
		
	}
	
}
